﻿using System;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
#region PathManipulation漏洞关闭
/*
namespace WebApplication
{
    /// <summary>
    /// 下载处理模块
    /// </summary>
    public class DowloadModule : IHttpModule
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此模块，
        /// 并向 IIS 注册此模块，然后才能使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //此处放置清除代码。
        }

        public void Init(HttpApplication context)
        {
            // 下面是如何处理 LogRequest 事件并为其
            // 提供自定义日志记录实现的示例
            context.BeginRequest += new EventHandler(context_BeginRequest);
            //context.LogRequest += new EventHandler(OnLogRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            var a = sender as HttpApplication;
            var context = a.Context;
            if (context.Request.QueryString["command"] == "download")
            {
                var path = context.Request.QueryString["path"].Replace("http://", "").Replace("https://", "");
                string virtrualPath = "~" + path.Remove(0, path.IndexOf("/"));
                //
                context.Response.Clear();
                context.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(Path.GetFileName(path))+"\""); 
                context.Response.WriteFile(virtrualPath);
                context.Response.End();
            }
        }

        #endregion

        public void OnLogRequest(Object source, EventArgs e)
        {
            //可以在此放置自定义日志记录逻辑
        }
    }
}*/
#endregion
